/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License,Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.wryproject.utils;

import com.example.wryproject.database.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.orm.Blob;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;

import java.util.List;

/**
 * 卡片数据库操作
 */
public class DatabaseUtils {

    private DatabaseUtils() {
    }

    public static OrmContext getOrmContext(Context context) {
        DatabaseHelper helper = new DatabaseHelper(context);
        OrmContext ormContext = helper.getOrmContext("MusicDatabase",
            "MusicDatabase.db", MusicDatabase.class);
        MyOrmObjectObserver observer = new MyOrmObjectObserver();

        // 调用registerEntityObserver方法注册一个观察者observer.
        ormContext.registerEntityObserver("cardData", observer);
        ormContext.registerEntityObserver("musicData", observer);
        return ormContext;
    }

    /**
     * 保存正在播放的音乐下标
     *
     * @param index 下标数据表
     * @param ormContext 数据连接
     */
    public static void insertPlayingIndex(PlayingIndex index, OrmContext ormContext) {
        OrmPredicates ormPredicates = ormContext.where(PlayingIndex.class);
        ormPredicates.equalTo("id", index.getId());
        List<CardData> cardDataList = ormContext.query(ormPredicates);
        if (cardDataList.isEmpty()) {
            ormContext.insert(index);
            ormContext.flush();
        } else {
            DatabaseUtils.updatePlayingIndex(index, ormContext);
        }
    }

    /**
     * 更新正在播放的音乐下标
     *
     * @param index 下标数据表
     * @param ormContext 数据连接
     */
    public static void updatePlayingIndex(PlayingIndex index, OrmContext ormContext) {
        OrmPredicates where = ormContext.where(PlayingIndex.class);
        where.equalTo("id", index.getId());
        List<PlayingIndex> indexList = ormContext.query(where);
        if (!indexList.isEmpty()) {
            indexList.get(0).setOldIndex(index.getOldIndex());
            ormContext.update(indexList.get(0));
            ormContext.flush();
        }
    }

    /**
     * 查询播放音乐的下标
     *
     * @param ormContext 数据连接
     */
    public static List<PlayingIndex> queryPlayingIndex(OrmContext ormContext) {
        OrmPredicates where = ormContext.where(PlayingIndex.class);
        return ormContext.query(where);
    }

    /**
     * 删除卡片
     *
     * @param cardId 卡片id
     * @param ormContext 数据连接
     */
    public static void deleteCardData(long cardId, OrmContext ormContext) {
        OrmPredicates where = ormContext.where(CardData.class);
        where.equalTo("cardId", cardId);
        List<CardData> cardDataList = ormContext.query(where);
        if (!cardDataList.isEmpty()) {
            ormContext.delete(cardDataList.get(0));
            ormContext.flush();
        }
    }

    /**
     * 更新卡片ID
     *
     * @param cardId 卡片id
     * @param ormContext 数据连接
     */
    public static void updateCardData(long cardId, OrmContext ormContext) {
        OrmPredicates where = ormContext.where(CardData.class);
        where.equalTo("cardId", cardId);
        List<CardData> cardDataList = ormContext.query(where);
        if (!cardDataList.isEmpty()) {
            cardDataList.get(0).setCardId(cardId);
            ormContext.update(cardDataList.get(0));
            ormContext.flush();
        }
    }

    /**
     * 更新音乐封面信息
     *
     * @param musicId 音乐id
     * @param ormContext 数据连接
     */
    public static void updateMusicData(int musicId, OrmContext ormContext, Blob cover) {
        OrmPredicates where = ormContext.where(MusicData.class);
        where.equalTo("musicId", musicId);
        List<MusicData> musicDataList = ormContext.query(where);
        if (!musicDataList.isEmpty()) {
            musicDataList.get(0).setMusicCover(cover);
            ormContext.update(musicDataList.get(0));
            ormContext.flush();
        }
    }

    /**
     * 新增卡片信息
     *
     * @param cardData 卡片对象
     * @param ormContext 数据连接
     */
    public static void insertCardData(CardData cardData, OrmContext ormContext) {
        OrmPredicates ormPredicates = ormContext.where(CardData.class);
        ormPredicates.equalTo("cardId", cardData.getCardId());
        List<CardData> cardDataList = ormContext.query(ormPredicates);
        if (cardDataList.isEmpty()) {
            ormContext.insert(cardData);
            ormContext.flush();
        } else {
            DatabaseUtils.updateCardData(cardData.getCardId(), ormContext);
        }
    }

    /**
     * 获取所有卡片
     *
     * @param ormContext 数据连接
     */
    public static List<CardData> queryAllCardData(OrmContext ormContext) {
        OrmPredicates where = ormContext.where(CardData.class);
        return ormContext.query(where);
    }

    /**
     * 根据播放位置获取单个卡片
     *
     * @param ormContext 数据连接
     */
    public static MusicData queryMusicData(int position, OrmContext ormContext) {
        OrmPredicates ormPredicates = ormContext.where(MusicData.class);
        List<MusicData> musicDataList = ormContext.query(ormPredicates);
        return musicDataList.get(position);
    }

    /**
     * 根据id获取单个卡片信息
     *
     * @param ormContext 数据连接
     */
    public static MusicData querySingleMusicData(int musicId, OrmContext ormContext) {
        OrmPredicates ormPredicates = ormContext.where(MusicData.class);
        ormPredicates.equalTo("musicId", musicId);
        List<MusicData> musicDataList = ormContext.query(ormPredicates);
        if (!musicDataList.isEmpty()) {
            return musicDataList.get(0);
        }
        return null;
    }

    /**
     * 查询所有music数据
     *
     * @param ormContext 数据连接
     */
    public static List<MusicData> queryAllMusicData(OrmContext ormContext) {
        OrmPredicates ormPredicates = ormContext.where(MusicData.class);
        return ormContext.query(ormPredicates);
    }

    /**
     * 插入歌曲数据到数据库
     *
     * @param musicData 数据
     * @param ormContext 数据连接
     * @param context 上下文
     */
    public static void insertMusicData(MusicData musicData, OrmContext ormContext, Context context) {
        OrmPredicates ormPredicates = ormContext.where(MusicData.class);
        ormPredicates.equalTo("musicId", musicData.getMusicId());
        List<MusicData> musicDataList = ormContext.query(ormPredicates);
        if (musicDataList.isEmpty()) {
            boolean insertSuccess = ormContext.insert(musicData);
            String title = musicData.getMusicTitle();
            if (insertSuccess) {
                boolean flushSuccess = ormContext.flush();
                if (!flushSuccess) {
                    new ToastDialog(context)
                        .setAlignment(LayoutAlignment.CENTER)
                        .setText("歌曲加载失败: " + title)
                        .show();
                }
            } else {
                new ToastDialog(context)
                    .setAlignment(LayoutAlignment.CENTER)
                    .setText("歌曲加载失败: " + title)
                    .show();
            }
        }
    }

    /**
     * 获取歌曲数据
     *
     * @param ormContext 数据库连接
     */
    public static List<MusicData> getAllMusicData(OrmContext ormContext) {
        OrmPredicates ormPredicates = ormContext.where(MusicData.class);
        return ormContext.query(ormPredicates);
    }
}
